package org.example.auth.service.authStrategy;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.example.auth.domain.EmailLoginBody;
import org.example.auth.domain.LoginUserDetails;
import org.example.auth.service.IAuthStrategy;
import org.example.auth.service.IAuthUserStrategy;
import org.example.auth.service.SysLoginService;
import org.example.common.core.constants.Constants;
import org.example.common.core.domain.model.LoginModel;
import org.example.common.core.domain.model.LoginUser;
import org.example.common.core.exception.ServiceException;
import org.example.common.core.utils.MessageUtils;
import org.example.common.core.utils.StringUtils;
import org.example.common.core.utils.ValidatorUtils;
import org.example.common.redis.utils.LoginHelper;
import org.example.common.redis.utils.RedisUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

/**
 * 邮箱登录
 */
@Service("email"+IAuthStrategy.BASE_NAME)
@RequiredArgsConstructor
public class EmailAuthStrategy implements IAuthStrategy {


    private final SysLoginService sysLoginService;
    private final String CODE_KEY="%s:emailCode:%s:%s";

    @Override
    public UserDetails authentication(UsernamePasswordAuthenticationToken authenticationToken) {
        Object object= authenticationToken.getDetails();
        EmailLoginBody loginBody=JSON.parseObject(JSON.toJSONString(object),EmailLoginBody.class);
        ValidatorUtils.validate(loginBody);
        sysLoginService.checkTenant(loginBody.getTenant_id());

        sysLoginService.checkLogin(loginBody.getGrant_type(),loginBody.getTenant_id(),
                loginBody.getEmail(),loginBody.getDevice_type(),
                ()->!RedisUtils.isExistsObject(String.format(CODE_KEY,loginBody.getTenant_id(),
                        loginBody.getEmail(),loginBody.getCode())));

        LoginUser loginUser= IAuthUserStrategy.getLoginUserByEmail(loginBody.getGrant_user().getCode(),
                loginBody.getTenant_id(),loginBody.getEmail());
        if(ObjectUtil.isNull(loginUser)){
            throw new ServiceException("用户不存在");
        }
        LoginModel loginModel=LoginModel.create();
        loginModel.setTimeout(loginBody.getExpires_in());
        loginModel.setToken(StringUtils.getUUID());
        loginModel.setDevice(loginBody.getDevice_type());
        loginModel.setGrantUser(loginBody.getGrant_user().getCode());
        LoginHelper.login(loginUser,loginModel);

        sysLoginService.recordLogininfor(loginUser.getTenantId(),loginUser.getUsername(),loginModel.getDevice(),Constants.SUCCESS,MessageUtils.message("user.login.success"));

        LoginUserDetails loginUserDetails= new LoginUserDetails(loginUser.getUsername(),
                loginUser.getPassword(),
                AuthorityUtils.createAuthorityList());
        loginUserDetails.setGrantUser(loginModel.getGrantUser());
        loginUserDetails.setDeviceType(loginModel.getDevice());
        loginUserDetails.setToken(loginModel.getToken());
        loginUserDetails.setTenantId(loginBody.getTenant_id());
        return loginUserDetails;
    }
}
